//
//  QHUpdateAlert.m
//  MUGH
//
//  Created by 李志良的mac mini on 2020/5/6.
//  Copyright © 2020 MUGH. All rights reserved.
//

#import "QHUpdateAlert.h"
static QHUpdateAlert * _sharedAlert = nil;

@implementation QHUpdateAlert

+ (QHUpdateAlert *)sharedAlert{
    @synchronized(self)
    {
        if (_sharedAlert == nil)
        {
            _sharedAlert = [[QHUpdateAlert alloc] init];
        }
    }
    return _sharedAlert;
    
}

-(id)init{
    self = [super init];
    if(self){
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI
{
    [[AppDelegate sharedAppDelegate].navigationViewController.view  addSubview:self.contentView];
    
    [self.contentView addSubview:self.maskView];
    [self.contentView addSubview:self.topRoketView];
    
    [self.topRoketView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.centerY.equalTo(self.contentView).offset(-95);
        make.height.mas_equalTo(195);
        make.centerX.mas_equalTo(self.contentView);
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setText:@"发现新版本"];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [self.topRoketView addSubview:titleLabel];
    
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.top.mas_equalTo(self.topRoketView.mas_top).offset(50);
        make.left.mas_equalTo(self.topRoketView.mas_left).offset(25);
        make.height.mas_equalTo(25);
    }];
    
    [self.contentView addSubview:self.bottomContentView];
    
    [self.bottomContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.76);
        make.top.mas_equalTo(self.topRoketView.mas_bottom).offset(-50);
        make.centerX.mas_equalTo(self.topRoketView.mas_centerX);
        make.height.mas_equalTo(155);
    }];
    

    [self.bottomContentView addSubview:self.alertContentLabel];
    
    [self.alertContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bottomContentView.mas_top).offset(5);
        make.left.mas_equalTo(self.bottomContentView.mas_left).offset(30);
        make.right.mas_equalTo(self.bottomContentView.mas_right).offset(-30);
        make.height.mas_equalTo(40);
    }];

    [self.bottomContentView addSubview:self.alertLabel];
    
    [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.alertContentLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.bottomContentView.mas_left);
        make.right.mas_equalTo(self.bottomContentView.mas_right);
        make.height.mas_equalTo(20);
    }];
    
    [self.bottomContentView addSubview:self.opraView];

    
    [self.opraView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bottomContentView.mas_left);
        make.top.mas_equalTo(self.alertLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(self.bottomContentView.mas_width);
        make.height.mas_equalTo(50);
    }];
    
    
    UIView * lineView = [[UIView alloc]init];
    [lineView setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.opraView.mas_width);
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(self.opraView.mas_left);
        make.height.mas_equalTo(1);
    }];
    
    UIView * v_Line = [[UIView alloc]init];
    [v_Line setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:v_Line];
    
    [v_Line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(self.opraView.mas_top);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
        make.centerX.mas_equalTo(self.opraView.mas_centerX);
    }];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(hideAlert) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitle:@"残忍拒绝" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.opraView addSubview:cancelButton];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(self.opraView.mas_left);
        make.right.mas_equalTo(v_Line.mas_left);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
    }];
    
    _retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_retryButton addTarget:self action:@selector(retryButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [_retryButton setTitle:@"立即升级" forState:UIControlStateNormal];
    [_retryButton setTitleColor:RGBA(90, 124, 188, 1) forState:UIControlStateNormal];
    [self.opraView addSubview:_retryButton];
    
    [_retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(v_Line.mas_right);
        make.right.mas_equalTo(self.opraView.mas_right);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
    }];
}

-(void)showAlertWithMessage:(NSString*)alertMessage;{
    [_contentView setAlpha:1.0];
    [self.alertContentLabel setText:alertMessage];
    //先算出Label高度
    float alertContentLabelHeight = [QHCommonTool caculateTheFormRowHeightWithLabelString:alertMessage LabelTextFontSize:14 LabelWidth:(SCREEN_WIDTH*0.76 - 60)];

        [self.alertContentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(alertContentLabelHeight);
        }];

    [self.bottomContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(alertContentLabelHeight+75);
    }];
}

-(void)showNetworkErrorMsg:(NSString *)errMsg{
    [_alertLabel setTextColor:RGBA(210, 210, 210, 1)];
    [_alertLabel setFont:[UIFont systemFontOfSize:14]];
    [_alertLabel setText:errMsg];
}

-(void)hideAlert{
    [_contentView setAlpha:0.0];
    if (self.refusedButtonTappedBlock) {
        self.refusedButtonTappedBlock();
    }
}

-(void)retryButtonTapped{
    [_contentView setAlpha:0.0];
//    [self hideAlert];
    if (self.updateButtonTappedBlock) {
        self.updateButtonTappedBlock();
    }
}


-(void)setUpdateProgress:(CGFloat)updateProgress{
//    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(updateProgress*self.progressBorderView.frame.size.width);
//    }];
//
    
}

-(UIView*)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        [_contentView setAlpha:0.0];
    }
    return _contentView;
}

-(UIView*)maskView{
    if(!_maskView){
        _maskView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        [_maskView setBackgroundColor:RGBA(0, 0, 0, 0.5)];
    }
    return _maskView;
}

-(UIImageView*)topRoketView{
    if(!_topRoketView){
        _topRoketView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"update_progress_rocket"]];
    }
    return _topRoketView;
}

#pragma mark - Properties

-(UIView*)bottomContentView{
    if(!_bottomContentView){
        _bottomContentView = [[UIView alloc]init];
        [_bottomContentView setBackgroundColor:[UIColor whiteColor]];
        [_bottomContentView.layer setCornerRadius:10];
        _bottomContentView.layer.masksToBounds=YES;
        _bottomContentView.clipsToBounds = YES;
    }
    return _bottomContentView;
}

-(UILabel*)alertLabel{
    if(!_alertLabel){
        _alertLabel = [[UILabel alloc]init];
        [_alertLabel setTextAlignment:NSTextAlignmentCenter];
        [_alertLabel setTextColor:RGBA(210, 210, 210, 1)];
        [_alertLabel setFont:[UIFont systemFontOfSize:14]];
        [_alertLabel setText:@"Wi-Fi环境下更新更快哦～"];
    }
    return _alertLabel;
}

-(UILabel*)alertContentLabel{
    if(!_alertContentLabel){
        _alertContentLabel = [[UILabel alloc]init];
        _alertContentLabel.textAlignment = NSTextAlignmentCenter;
        [_alertContentLabel setTextColor:RGBA(90, 90, 90, 1)];
        _alertContentLabel.numberOfLines = 2;
        [_alertContentLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _alertContentLabel;
}

-(UIView*)opraView{
    if(!_opraView){
        _opraView = [[UIView alloc]init];
    }
    return _opraView;
}
@end
